package controllers;

import models.ApplicationUser;
import play.Play;
import play.i18n.Lang;
import play.libs.OpenID;
import play.mvc.Before;
import play.mvc.Controller;

import java.util.Arrays;

public class Application extends Controller {

    @Before(unless = {"login", "authenticate"})
    static void checkAuthentication() {
        if (!session.contains("uid")) {
            session.clear();
            login();
        }
        ApplicationUser user = ApplicationUser.findById(Long.parseLong(session.get("uid")));
        if (user == null) {
            session.clear();
            login();
        }
    }

    public static void login() {
        session.clear();
        render();
    }

    public static void logout() {
        session.clear();
        login();
    }

    public static void authenticate(String user) {
        if (OpenID.isAuthenticationResponse()) {
            OpenID.UserInfo userInfo = OpenID.getVerifiedID();
            if (userInfo == null) {
                flash.put("error", "Authentication failed");
                login();
            } else {
                String id = userInfo.id;

                if (ApplicationUser.find("byOpenId", id).first() == null) {
                    ApplicationUser applicationUser = new ApplicationUser(id, userInfo.extensions);
                    applicationUser.save();
                    applicationUser.init();
                }
                ApplicationUser applicationUser = ApplicationUser.find("byOpenId", id).first();
                session.put("uid", applicationUser.id);
                index();
            }
        } else {
            if (user.endsWith("@gmail.com")) {
                OpenID.id("https://www.google.com/accounts/o8/id")
                        .required("email", "http://axschema.org/contact/email")
                        .required("name", "http://axschema.org/namePerson")
                        .required("birthDate", "http://axschema.org/birthDate")
                        .required("country", "http://axschema.org/contact/country/home")
                        .required("language", "http://axschema.org/pref/language")
                        .verify();
            } else {
                OpenID.id(user)
                        .required("email", "http://axschema.org/contact/email")
                        .required("name", "http://axschema.org/namePerson")
                        .required("birthDate", "http://axschema.org/birthDate")
                        .required("country", "http://axschema.org/contact/country/home")
                        .required("language", "http://axschema.org/pref/language")
                        .verify();
            }
        }
    }

    public static void index() {
        ApplicationUser user = ApplicationUser.findById(Long.parseLong(session.get("uid")));
        Lang.change(Lang.get());
        String email = user.email;
        String name = user.name;
        String dateOfBirthday = user.dateOfBirthday;
        String country = user.country;
        String language = user.language;
        render(email, dateOfBirthday, country, language, name);
    }

    public static void setLang(String lang) {
        String[] langsArray = Play.configuration.getProperty("application.langs").split(",");
        if (!Arrays.asList(langsArray).contains(lang)) {
            return;
        }
        Lang.change(lang);
    }


}
